/*
 * Copyright (c) 2009 XMock contributors
 * This program is made available under the terms of the Apache License.
 */
package org.xmock.controller;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;

public class LocalMockObjectManager implements MockObjectManager
{
	private static final Logger logger = Logger.getLogger(LocalMockObjectManager.class);
	private static final String signatureReplaceRegex = "[ (),:]";
	public static LocalMockObjectManager instance;
	public static String defaultObjectLocation = ".";
	
	public String objectLocation;
	
	public static LocalMockObjectManager getInstance()
	{
		if(instance == null)
		{
			instance = new LocalMockObjectManager();
		}
		return instance;
	}
	
	private LocalMockObjectManager()
	{
		init();
	}
	
	private void init()
	{
		MockProperties properties = MockProperties.getInstance();
		objectLocation = properties.getProperty("mock.object.store.location");
		if(objectLocation==null)
		{
			logger.error("Could not find object store location");
			objectLocation = defaultObjectLocation;
			return;
		}
	}
	
	public Object getMockObject(String methodSignature, String caseName, String caseId) 
	{
		return getMockObject(methodSignature, caseName, caseId, null);		
	}

	public Object getMockObject(String methodSignature, String caseName,
			String caseId, String recordName) {
		Object object = null;
		String fileName = getFileName(methodSignature, caseName, caseId, recordName);
		File objectFile = new File(fileName);
		if(objectFile.exists())
		{
			XStream xstream = new XStream();
			try {
				FileReader fr = new FileReader(objectFile);
				object = xstream.fromXML(fr);
			} catch (Exception exception)
			{
				logger.error("Error reading mock object", exception);
			}
		}
		return object;
	}

	public boolean isMockObjectPresent(String methodSignature, String caseName, String caseId) 
	{
		String fileName = getFileName(methodSignature, caseName, caseId);
		return isMockObjectPresent(fileName);
	}

	public void saveMockObject(String methodSignature, String caseName, String caseId, Object mockObject) 
	{
		saveMockObject(methodSignature, caseName, caseId, null, mockObject);		
	}

	public void saveMockObject(String methodSignature, String caseName,
			String caseId, String recordName, Object mockObject) {
		String fileName = getFileName(methodSignature, caseName, caseId, recordName);
		File objectFile = new File(fileName);
		XStream xstream = new XStream();
		try 
		{
			FileWriter fw = new FileWriter(objectFile);
			xstream.toXML(mockObject, fw);
		} 
		catch (Exception exception) 
		{
			logger.error("Error saving mock object", exception);
		}
	}
		
	private String getFileName(String methodSignature, String caseName,
			String caseId)
	{
		return getFileName(methodSignature, caseName, caseId, null);			
	}
	
	private String getFileName(String methodSignature, String caseName,
			String caseId, String recordName)
	{
		StringBuffer objectFileSignature = new StringBuffer(methodSignature).append("_").append(caseName).append("_").append(caseId);
		if(recordName != null)
		{
			objectFileSignature.append("_").append(recordName);
		}
		String fileName = objectFileSignature.toString().replaceAll(signatureReplaceRegex,"_");
		return objectLocation + File.separator + fileName + ".xml";			
	}
	
	private boolean isMockObjectPresent(String fileName)
	{
		File objectFile = new File(fileName);
		return objectFile.exists();
	}

}
